<?php defined('SYSPATH') OR die('No direct access allowed.');

class Members_Controller extends Forum_Controller {

	public $actions = array();

	public function __construct() {
		parent::__construct();
		$this->actions = array
		(
			'show'			=> 'members.actions.view',
			'comments'	=> 'members.actions.comments',
			'posts'			=> 'members.actions.posts',
		);
	}

	public function index() {
		// shows all members info
		$page_num = $this->input->get('page', 1);
		$per_page = Kohana::config('forum.per_page.users');
		$offset = ($page_num - 1)*$per_page;
		$user = ORM::factory('forum_user');
		$users = $user->limit($per_page, $offset)->find_all();
		$pages = Pagination::factory(array
			(
				'style' => Kohana::config('forum.pagination_style'),
				'items_per_page' => $per_page,
				'query_string' => 'page',
				'total_items' => $user->count_last_query(),
			));
		$this->template->head['title'] = Kohana::lang('members.title');
		$this->template->content = View::factory('forum/show_members')
			->bind('users', $users)
			->bind('pages', $pages);
	}

	public function show($id = NULL) {
		if (is_null($id)) return $this->index();
		$user = ORM::factory('forum_user', $id);
		if (!$user->loaded) {
			message::add(Kohana::lang('members.user.not_found'), 'error');
			Event::run('system.404');
		}
		$this->template->head['title'] = Kohana::lang('members.profile.title').$user->username;
		$this->template->content = View::factory('forum/show_member')->bind('user', $user);
		$this->template->content->member_content = View::factory('forum/blocks/member_profile')
			->bind('user', $user);
	}

	public function comments($id = NULL) {
		$user = ORM::factory('forum_user', $id);
		if (!$user->loaded) {
			message::add(Kohana::lang('members.user.not_found'), 'error');
			Event::run('system.404');
		}
		$page_num = $this->input->get('page', 1);
		$per_page = Kohana::config('forum.per_page.usercomments');
		$offset = ($page_num - 1)*$per_page;
		$comments = ORM::factory('forum_comment')->limit($per_page, $offset)->orderby('created', 'DESC')->get_by_user($user->id);
		$pages = Pagination::factory(array
			(
				'style' => Kohana::config('forum.pagination_style'),
				'items_per_page' => $per_page,
				'query_string' => 'page',
				'total_items' => $user->userinfo->comment_count,
			));
		$this->template->head['title'] = Kohana::lang('members.comments.title')." ".$user->username;
		$this->template->content = View::factory('forum/show_member')->bind('user', $user);
		$this->template->content->member_content = View::factory('forum/blocks/member_comments')
			->bind('comments', $comments)
			->bind('user', $user)
			->bind('pages', $pages);
	}

	public function posts($id = NULL) {
		$user = ORM::factory('forum_user', $id);
		if (!$user->loaded) {
			message::add(Kohana::lang('members.user.not_found'), 'error');
			Event::run('system.404');
		}
		$page_num = $this->input->get('page', 1);
		$per_page = Kohana::config('forum.per_page.userposts');
		$offset = ($page_num - 1)*$per_page;
		$posts = ORM::factory('forum_post')->limit($per_page, $offset)->orderby('created', 'DESC')->get_by_user($user->id);
		$pages = Pagination::factory(array
			(
				'style' => Kohana::config('forum.pagination_style'),
				'items_per_page' => $per_page,
				'query_string' => 'page',
				'total_items' => $user->userinfo->post_count,
			));
		$this->template->head['title'] = Kohana::lang('members.posts.title')." ".$user->username;
		$this->template->content = View::factory('forum/show_member')->bind('user', $user);
		$this->template->content->member_content = View::factory('forum/blocks/member_posts')
			->bind('posts', $posts)
			->bind('user', $user)
			->bind('pages', $pages);
	}

	public function __call($method, $args) {
		if (isset($args['id'])){
			if (method_exists($this, $args['id'])) {
				$this->$args['id']($method);
			}
			else {
				message::add(Kohana::lang('members.actions.unknown'), 'error');
				Event::run('system.404');
			}
		}
		else {
				$this->show($method);
		}
		//}
	}

}